package game.sprite;

import engine.j2d.Graphics;
import engine.j2d.Image;

public class AnimationSprite
{

	private Image image[];
	private int height[];
	private int width[];
	private int counter = 0;
	private int frames = 0;
	private int delay;
	private boolean loop;
	private float x;
	private float y;
	private boolean zustand = true;
	
	public AnimationSprite(Image image[], float x, float y, int delay,
			boolean loop)
	{
		this.image = image;
		this.x = x;
		this.y = y;
		this.loop = loop;
		this.delay = delay;
		width = new int[image.length];
		height = new int[image.length];

		for (int i = 0; i < image.length; i++)
		{
			while (image[i].getWidth() < 0)
				Thread.yield();

			width[i] = image[i].getWidth();
			height[i] = image[i].getHeight();
		}
	}

	public void render(Graphics g, float offsetx, float offsety,
			boolean stopAktion)
	{
		if (counter >= image.length)
			return;
		g.drawImage(image[counter], x + offsetx, y + offsety, stopAktion);
		frames++;

		if (frames >= delay)
		{
			if (loop)
				counter = (counter + 1) % image.length;
			else
				counter++;

			frames = 0;
		}
	}
	
	public boolean isFinishAnimation() {
		return counter >= image.length;
	}
	/**
	 *  Nur bei Explostionen verwenden
	 * */
	public void renderExplo(Graphics g, float offsetx, float offsety,
			boolean stopAktion)
	{
		if(zustand)
		{
			render(g, offsetx, offsety, stopAktion);
			if(counter>image.length) zustand= false;
		}
	}
	public void setToggl(boolean zustand)
	{
		this.zustand=zustand;
	}
	public void render()
	{
		if (counter >= image.length)
			return;
		image[counter].draw(x, y);
		frames++;

		if (frames >= delay)
		{
			if (loop)
				counter = (counter + 1) % image.length;
			else
				counter++;

			frames = 0;
		}
	}

	public void render(float xc, float yc)
	{
		if (counter >= image.length)
			return;
		image[counter].draw(x + xc, y + yc);
		frames++;

		if (frames >= delay)
		{
			if (loop)
				counter = (counter + 1) % image.length;
			else
				counter++;

			frames = 0;
		}
	}

	public void setX(float x)
	{
		this.x = x;
	}

	public void setY(float y)
	{
		this.y = y;
	}

	public float getX()
	{
		return x;
	}

	public float getY()
	{
		return y;
	}
}
